package com.estate.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.estate.dao.CommentDao;
import com.estate.vo.Vo;
import com.estate.vo.impl.ForumCommentVo;
import com.estate.vo.impl.UserInfoVo;

public class CommentDaoImpl implements CommentDao {
	
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory(){
		return this.sessionFactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory=sessionFactory;
	}
	public boolean addVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().save(vo);
		return true;
	}

	public boolean deleteVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().delete(vo);
		return true;
	}

	public List<Vo> findAll() {
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from ForumCommentVo");
		List<Vo> voList = query.list();
		return voList;
	}

	public Vo findByPK(Object pk) {
		Vo vo = (Vo) this.getSessionFactory().getCurrentSession().get(ForumCommentVo.class, (Integer)pk);
		return vo;
	}

	public boolean updateVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().merge(vo);
		return true;
	}

	public List<ForumCommentVo> getForumCommentList(int forumId) {
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from ForumCommentVo where forumId=:forumId order by commentDate").setInteger("forumId", forumId);
		List<ForumCommentVo> voList = query.list();
		return voList;
	}

	public boolean deleteForumComments(List<ForumCommentVo> commentList) {
		
		this.getSessionFactory().getCurrentSession().clear();
		for(int i = 0;i<commentList.size();i++){
			this.deleteVo(commentList.get(i));
		}
		return true;
	}

	public List<ForumCommentVo> getUserCommentList(UserInfoVo userInfo) {
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from ForumCommentVo where commentUser=:commentUser").setEntity("commentUser", userInfo);
		List<ForumCommentVo> commentList = query.list();
		return commentList;
	}

}
